<?php

namespace Admin\Controller;
use Think\Controller;

class ArticleController extends Controller{

    function __initialize(){

        $this->article_model=D('Article');
    }

    function showlist(){

        $article_info=$this->article_model->order('id desc')->select();
        $this->assign('article_info',$article_info);
        $this->display();
    }
    function add(){

        $article=new \Model\ArticleModel();
        if(!empty(I('POST.content'))){

            $a=strpos($_POST['content'],'src="');
            $b=strpos($_POST['content'],'.jpg');
            $path=substr($_POST['content'],$a+5,$b-$a-1);
            $_POST=$_POST+array('photo_path'=>"$path");
            $info=$_POST;
            $return=$article->add($info);
            if($return){
                var_dump($info);
//                $this->redirect('showlist',array(),'12',"添加成功");
            }else{
                $this->redirect('add',array(),'2','添加失败');
            }
        }
        $this->display();
    }

    function shanchu($id){
        $article=M('Article');
        $return=$article->delete($id);
        if($return){
            $this->redirect('showlist',array(),'0','删除成功');
        }else{
            $this->redirect('showlist',array(),'2','删除失败');
        }
        $this->display();
    }
    function update($id){
        $article=D('Article');
        if(!empty(I('POST.'))){
            $return=$article->where("id=$id")->save($_POST);
            if($return){
                $this->redirect('showlist',array(),'1','<h2>修改成功</h2>');
            }else{
                $this->redirect('update',array('id'=>$id),'5','<h2>修改失败</h2>');
            }
        }
        $info=$article->find($id);
        $this->assign('info',$info);
        $this->display();
    }
}

?>